package com.test;

import com.cn.jpush.api.JPushClient;
import com.cn.jpush.api.common.resp.APIConnectionException;
import com.cn.jpush.api.common.resp.APIRequestException;
import com.cn.jpush.api.push.PushResult;
import com.cn.jpush.api.push.model.Message;
import com.cn.jpush.api.push.model.Options;
import com.cn.jpush.api.push.model.Platform;
import com.cn.jpush.api.push.model.PushPayload;
import com.cn.jpush.api.push.model.audience.Audience;
import com.cn.jpush.api.push.model.audience.AudienceTarget;
import com.cn.jpush.api.push.model.notification.AndroidNotification;
import com.cn.jpush.api.push.model.notification.IosNotification;
import com.cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by yangluo on 2015/7/9.
 */
public class JdPushdynamic  {
    protected static final Logger LOG = LoggerFactory.getLogger(JdPushdynamic.class);

    public static  String REGISTRATION_ID = "0900e8d85ef";

    public static  String TAG = "tag_api";

    public  static JPushClient jpushClient=null;

    //��������


    /**
     *
     * @param appKey
     * @param masterSecret
     */
    public static void testSendPush(String appKey ,String masterSecret, information informatio) {



        jpushClient = new JPushClient(masterSecret, appKey, 3);

        // HttpProxy proxy = new HttpProxy("localhost", 3128);
        // Can use this https proxy: https://github.com/Exa-Networks/exaproxy


        // For push, all you need do is to build PushPayload object.
        //PushPayload payload = buildPushObject_all_all_alert();
        //�������͵����ݣ����������Ȳ���ȫ������
        //PushPayload payload=buildPushObject_all_alias_alert();

        //buildPushObject_android_tag_alertWithTitle
        //����  ��׿�û�
        //PushPayload payload=buildPushObject_android_tag_alertWithTitle();



        //buildPushObject_ios_tagAnd_alertWithExtrasAndMessage
        //buildPushObject_all_all_alert
        //buildPushObject_ios_audienceMore_messageWithExtras
        PushPayload payload= buildPushObject_all_alias_alert(informatio);
        try {
            System.out.println(payload.toString());
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result+"................................");

            //LOG.info("Got result - " + result);

        } catch (APIConnectionException e) {
            //LOG.error("Connection error. Should retry later. ", e);

        } catch (APIRequestException e) {
           // LOG.error("Error response from JPush server. Should review and fix it. ", e);
           // LOG.info("HTTP Status: " + e.getStatus());
           // LOG.info("Error Code: " + e.getErrorCode());
           // LOG.info("Error Message: " + e.getErrorMessage());
           // LOG.info("Msg ID: " + e.getMsgId());
        }
    }

    /**
     * ����������Ϣ
     * @return
     */
    public PushPayload buildPushObject_all_all_alert(information informatio) {
        return PushPayload.alertAll(informatio.ALERT);
    }

    /**
     * �������û�����������Ϣ
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(information informatio) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//���ý��ܵ�ƽ̨
                .setAudience(Audience.all())//Audience����Ϊall��˵�����ù㲥��ʽ���ͣ������û������Խ��յ�
                .setNotification(Notification.alert(informatio.ALERT))
                .build();
    }

    /**
     *  ��׿�û�������Ϣ
     * @return
     */
    public static PushPayload buildPushObject_android_tag_alertWithTitle(information informatio) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(informatio.ALERT, informatio.TITLE, null))
                .build();
    }

    /**
     *  ��׿��ƻ���û�������Ϣ
     * @return
     */
    public static PushPayload buildPushObject_android_and_ios(information informatio) {
        return PushPayload.newBuilder()//���
                .setPlatform(Platform.android_ios())//����ƽ̨
                .setAudience(Audience.tag("tag1"))//����
                .setNotification(Notification.newBuilder()
                        .setAlert("alert content")
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle("Android Title").build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("extra_key", "extra_value").build())
                        .build())
                .build();
    }

    /**
     *  ��ƻ���û�������ʱ��Ϣ
     * @return
     */
    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(information informatio) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and("tag1", "tag_all"))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(informatio.ALERT)
                                .setBadge(5)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                .setMessage(Message.content(informatio.MSG_CONTENT))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }

    /**
     * ��ƻ���û����Ͷ�����Ϣ
     * @return
     */
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(information informatio) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
                        .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(informatio.MSG_CONTENT)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }
}
